package com.example.demox.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/zcc")
public class zcc {

    @RequestMapping(value = "/uploadApk",method = RequestMethod.POST)
    @ResponseBody
    public void uploadApkController(HttpServletRequest request) throws IOException {
        System.out.println("进入zcc");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        //获取文件
        //MultipartFile uploadPdf = multipartRequest.getFile("zxc");
        Map<String,MultipartFile> map= multipartRequest.getFileMap();
        MultipartFile uploadPdf=map.get("zxc");
        //获取字符串参数
        String uploadPdf1 = multipartRequest.getParameter("name");
        System.out.println("名字是:"+uploadPdf1);
        if(uploadPdf != null) {
            //获取文件名
            System.out.println("进入"+uploadPdf.getOriginalFilename());;
            //获取文件后缀名
            String suffix = uploadPdf.getOriginalFilename().substring(uploadPdf.getOriginalFilename().lastIndexOf("."));
            //需要保存的文件对象
            File targetFile = new File("C:\\Users\\zcc\\Desktop",  "zsd" + suffix);
            //判断该文件对象在磁盘上是否存在
            if(!targetFile.exists()){
                //磁盘上不存在，则进行文件的创建
                targetFile.mkdirs();
            }
            //文件从内存中保存到磁盘上(该方法是springmvc封装的方法)
            uploadPdf.transferTo(targetFile);
        }

    }




}
